import "dart:io";
import "dart:math";

import "package:flutter/foundation.dart";
import "package:flutter_image_compress/flutter_image_compress.dart";
import "package:heic_to_jpg/heic_to_jpg.dart";
import "package:pc/Util/FileUtils.dart";
import "package:video_compress/video_compress.dart";

class CompressUtils {
  static String _getFileExtName(String path) => path.split("/").last.split(".").last.toLowerCase();

  static Future<String> compressImage(String imagePath) async {
    int fileSizeToInSampleSize(String path) {
      if (_getFileExtName(path) == "png") return 1;
      int fileSize = File(path).lengthSync();
      if (fileSize < 100 * 1000)
        return 1;
      else if (fileSize < 1000 * 1000)
        return 1;
      else if (fileSize < 2000 * 1000)
        return 2;
      else
        return 3;
    }

    int fileSizeToQuality(String path) {
      if (_getFileExtName(path) == "png") return 100;
      int fileSize = File(path).lengthSync();
      if (fileSize < 100 * 1000)
        return 100;
      else if (fileSize < 1000 * 1000)
        return 80;
      else if (fileSize < 2000 * 1000)
        return 60;
      else
        return 40;
    }

    //根据原文件大小，压缩大小
    int fileSizeToMinSize(String path) {
      if (_getFileExtName(path) == "png") return 1080;
      int fileSize = File(path).lengthSync();
      if (fileSize > 10 * 1000 * 1000)
        return 500;
      else if (fileSize > 5 * 1000 * 1000)
        return 550;
      else if (fileSize > 2 * 1000 * 1000)
        return 600;
      else if (fileSize > 1 * 1000 * 1000)
        return 720;
      else
        return 1080;
    }

    File file = File(imagePath);
    try {
      final extName = _getFileExtName(file.path);
      if (extName == "gif") return file.path;
      final path = (["heif", "heic"].contains(extName) ? await HeicToJpg.convert(file.absolute.path) : file.absolute.path)!;
      final format = (extName == "png" ? CompressFormat.png : CompressFormat.jpeg);
      final compressPath = "${await FileUtils.getDir(FileDir.compress_image)}${DateTime.now().microsecondsSinceEpoch}_${Random().nextInt(99999999)}.$extName";
      switch (defaultTargetPlatform) {
        case TargetPlatform.android:
          return (await FlutterImageCompress.compressAndGetFile(
            path,
            compressPath,
            quality: fileSizeToQuality(path),
            format: format,
            inSampleSize: fileSizeToInSampleSize(path),
          ))!
              .path;
        case TargetPlatform.iOS:
          return (await FlutterImageCompress.compressAndGetFile(
            path,
            compressPath,
            quality: fileSizeToQuality(path),
            format: format,
            minHeight: fileSizeToMinSize(path),
            minWidth: fileSizeToMinSize(path),
          ))!
              .path;
        default:
          return file.path;
      }
    } catch (e) {
      return file.path;
    }
  }

  static Future<String> compressVideo(String videoPath) async {
    File file = File(videoPath);
    try {
      if (!["mp4", "avi", "mpeg"].contains(_getFileExtName(file.path))) return file.path;
      return (await VideoCompress.compressVideo(
        file.path,
        quality: VideoQuality.DefaultQuality,
        deleteOrigin: false,
        includeAudio: true,
      ))!
          .file!
          .path;
    } catch (e) {
      return file.path;
    }
  }
}
